package com.hndzyu.yd.my;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.bean.UserLM;
import com.hndzyu.yd.customtask.ApprovalActivity;
import com.hndzyu.yd.h5.AdListActivity;
import com.hndzyu.yd.ui.BindingPhoneActivity;
import com.hndzyu.yd.ui.LoginActivity;
import com.hndzyu.yd.url.Path;
import com.hndzyu.yd.utils.OkHttpDownloadJsonUtil;
import com.hndzyu.yd.utils.SharedPreferencesUtils;
import com.hndzyu.yd.utils.Util;
import com.hndzyu.yd.vip.MyPDDActivity;
import com.hndzyu.yd.widget.BindDialog;
import com.hndzyu.yd.widget.CircleImageView;
import com.hndzyu.yd.widget.CodePopupWindow;
import com.hndzyu.yd.widget.ExitDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MyFragment extends Fragment {

    View view;
    TextView userid;
    TextView dangqianyuer;
    Unbinder unbinder;
    @BindView(R.id.nickname)
    TextView nickname;
    @BindView(R.id.headimg)
    CircleImageView headimg;
    @BindView(R.id.tixianarea)
    RelativeLayout tixianarea;
    @BindView(R.id.tv_phone_num)
    TextView tvPhoneNum;
    private View mypublish;
    private View mypdd;
    String url = "";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_my, container, false);
        userid = (TextView) view.findViewById(R.id.userid);
        dangqianyuer = (TextView) view.findViewById(R.id.dangqianyuer);
        //个人中心
        view.findViewById(R.id.ll_myinfo_editinfo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserBean.uid != 0) {
                    startActivity(new Intent(getActivity(), MyInfoActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            }
        });
        view.findViewById(R.id.headimg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserBean.uid != 0) {
                    startActivity(new Intent(getActivity(), MyInfoActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            }
        });
        //我的消息
        view.findViewById(R.id.rl_myinfo_msg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), MyMessageActivity.class));
            }
        });
        //我发布的任务
        mypublish = view.findViewById(R.id.mypublish);
        mypublish.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), ApprovalActivity.class));
            }
        });
        //我的返佣
        mypdd = view.findViewById(R.id.mypdd);
        mypdd.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), MyPDDActivity.class));
            }
        });
        //绑定手机
        view.findViewById(R.id.bangding_phone).setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), BindingPhoneActivity.class));
            }
        });

        //商务合作
        view.findViewById(R.id.shangwu).setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), CooperationActivity.class));
            }
        });
        //常见问题
        view.findViewById(R.id.changjian).setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), CommonProblemActivity.class));
            }
        });
        //任务记录
        view.findViewById(R.id.renwujilurarea).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserBean.uid != 0) {
                        startActivity(new Intent(getActivity(), TaskRecordActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            }
        });
        //提现
        view.findViewById(R.id.tixianarea).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserBean.uid != 0) {
                    if ("".equals(UserBean.mobile)) {
                        final BindDialog bindDIalog = new BindDialog(getContext());
                        bindDIalog.setBindListener(new BindDialog.BindListener() {
                            @Override
                            public void chooseTrue() {
                                startActivity(new Intent(getActivity(), BindingPhoneActivity.class));
                                bindDIalog.dismiss();
                            }

                            @Override
                            public void chooseFalse() {
                                bindDIalog.dismiss();
                            }
                        });
                        bindDIalog.show();
                        bindDIalog.setContent();

                    }else {
                        startActivity(new Intent(getActivity(), TiXianActivity.class));
                    }

                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            }
        });
        //提现记录
        view.findViewById(R.id.wodetixianarea).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserBean.uid != 0) {
                    startActivity(new Intent(getActivity(), TiRecordActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            }
        });

        //邀请好友
        view.findViewById(R.id.yaoqing).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserBean.uid != 0) {
                    startActivity(new Intent(getActivity(), ShareCodeActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            }
        });
        //二维码
        view.findViewById(R.id.rl_ewm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserBean.uid != 0) {

                    CodePopupWindow popupWindow = new CodePopupWindow(getActivity());
                    popupWindow.showDown(nickname);
                } else {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            }
        });
        view.findViewById(R.id.iv_myinfo_setting).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), SettingActivity.class));
            }
        });
        view.findViewById(R.id.iv_sl).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                url = "";
                if (!Util.isPackageExist(getContext(),"de.robv.android.xposed.installer")){
                    url = "http://39.98.225.37:999/XposedInstaller_3.1.4.apk";
                } else if ("".equals(url) && !Util.isPackageExist(getContext(),"com.example.myapplication")){
                    url = "http://39.98.225.37:999/jk3.apk";
                } else if ("".equals(url) && !Util.isPackageExist(getContext(),"com.touchsprite.android")){
                    url = "http://39.98.225.37:999/TouchSprite_v3.0.1_signed.apk";
                } else {
                    if ("".equals(url)) {
                        Util.startActivityByPackageName(getContext(), "com.touchsprite.android");
                        return;
                    }
                }
                final ExitDialog mdelDialog = new ExitDialog(getContext());
                mdelDialog.setExitListener(new ExitDialog.ExitListener() {
                    @Override
                    public void chooseTrue() {
                        //确定清除
                        mdelDialog.dismiss();
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    }

                    @Override
                    public void chooseFalse() {
                        //取消
                        mdelDialog.dismiss();
                    }
                });
                mdelDialog.show();
                mdelDialog.downQQ();

            }
        });
        view.findViewById(R.id.zhb).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), AdListActivity.class));
            }
        });
        unbinder = ButterKnife.bind(this, view);
        initData();
        return view;
    }

    protected ImmersionBar mImmersionBar;

    /**
     * 初始化沉浸式
     */
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).init();
        mImmersionBar
                .statusBarDarkFont(true, 0.2f)
                .init();
    }

    @SuppressLint("SetTextI18n")
    public void initData() {
        setMsg();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initImmersionBar();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (UserBean.level>0){
            mypublish.setVisibility(View.VISIBLE);
            mypdd.setVisibility(View.VISIBLE);
        }
        setMsg();
    }

    @SuppressLint("SetTextI18n")
    private void setMsg() {
        if (UserBean.uid != 0) {
            userid.setText(UserBean.uid + "");
            if (!"".equals(UserBean.nickname)) {
                nickname.setText(UserBean.nickname);
            } else {
                nickname.setText("新用户");
            }
            if (!"".equals(UserBean.mobile)){
                tvPhoneNum.setText(UserBean.mobile.substring(0,3) + "****" + UserBean.mobile.substring(7,UserBean.mobile.length()));
            }
            Glide.with(view.getContext()).load(Path.BASE_URL + "/" + UserBean.headimg)
                    .apply(new RequestOptions().error(R.mipmap.logo2)).into(headimg);
            money(UserBean.uid);
        } else {
            userid.setText("00000");
            nickname.setText("未登录");
            dangqianyuer.setText("0.00");
        }
    }

    private void money(int uid) {
        OkHttpDownloadJsonUtil.downloadJson(view.getContext(), Path.money + "?uid=" + uid, new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {

                if (!"1".equals(json) && !"2".equals(json)) {
                    try {
                        Gson gson = new Gson();
                        UserLM userLM = gson.fromJson(json, UserLM.class);
                        UserBean.balance = userLM.getInfo().getBalance();
                        UserBean.nickname = userLM.getInfo().getNickname();
                        UserBean.gao = userLM.getInfo().getGaotype();
                        if (UserBean.level > 0) {
                            mypublish.setVisibility(View.VISIBLE);
                            mypdd.setVisibility(View.VISIBLE);
                        }
                        nickname.setText(UserBean.nickname);
                        SharedPreferencesUtils.setParam(view.getContext(), "nickname", UserBean.nickname);
                        SharedPreferencesUtils.setParam(view.getContext(), "balance", UserBean.balance);
                        dangqianyuer.setText(UserBean.balance);
                    }catch (Exception e){
                        Log.d("____",json);
                    }
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        if (mImmersionBar != null) {
            mImmersionBar.destroy();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (UserBean.uid!=0) {
            money(UserBean.uid);
        }
        if (!hidden && mImmersionBar != null) {
            ImmersionBar.with(this)
                    .statusBarDarkFont(true, 0.2f)
                    .init();
        }
    }
}
